package com.mjh.zhujianju.service.supervise.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.supervise.SupervisionUsersMapper;
import com.mjh.zhujianju.model.supervise.SupervisionUsersEO;
import com.mjh.zhujianju.service.supervise.SupervisionUsersService;

@Service
public class SupervisionUsersServiceImpl extends BaseServiceImpl<SupervisionUsersEO>
		implements SupervisionUsersService {

	@Resource
	private SupervisionUsersMapper supervisionUsersMapper;
	
	@Resource
	private FileService fileService;
	
	@Resource
	private SysUserService userService;

	public SupervisionUsersServiceImpl(SupervisionUsersMapper supervisionUsersMapper) {
		super(supervisionUsersMapper);
	}

	@Override
	public List<SupervisionUsersEO> getUserListBysupervisionId(Long supervisionId) {
		if(supervisionId==null) {
			return null;
		}
		List<Long> userIds = new ArrayList<Long>();
		List<SupervisionUsersEO> list =	supervisionUsersMapper.selectUserListBysupervisionId(supervisionId);
		if(!XTListUtils.isEmpty(list)) {
			for (SupervisionUsersEO supervisionUsersEO : list) {
				userIds.add(supervisionUsersEO.getUserId());
			}
			//头像
			Map<Long, FileEO> map  =	fileService.getMapTopOne(userIds, SysUser.class.getName(), null);
			Map<Long, SysUser> mapUser =	userService.getMapByIdList(userIds);
				for (SupervisionUsersEO supervisionUsersEO : list) {
					if(map!=null) {
					FileEO file=  map.get(supervisionUsersEO.getUserId());
					if(file!=null) {
						supervisionUsersEO.setHeadImage(file.getId());
					}
					}
					if(mapUser!=null) {
						SysUser user =	mapUser.get(supervisionUsersEO.getUserId());
						if(user!=null) {
							supervisionUsersEO.setNickname(user.getNickname());
						}
					}
				}	
			
		}
		return list;
	}

	@Override
	public void delByPrimarySupervisionId(Long id) throws XTBusinessException {
		if(id==null) {
			return;
		}
		this.supervisionUsersMapper.delByPrimarySupervisionId(id);
	}

	@Override
	public List<SupervisionUsersEO> getSupervisionUserBySupervisionIds(List<Long> ids) {
		return this.supervisionUsersMapper.selectUserListBysupervisionIds(ids);
	}

}
